home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / AHDI / SYQUEST / SQHDX / WINCAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  4.8 KB  |  332 lines

  1. /* wincap.c */
  2.  
  3. /*
  4.  * Winchester Disk `capability' file parser.
  5.  *
  6.  *    int wgetent(name);
  7.  *    char *name;
  8.  *
  9.  *    int wallents(buf);
  10.  *    char *buf;
  11.  *
  12.  *    int wgetnum(id, anum);
  13.  *    char *id;
  14.  *    int *anum;
  15.  *
  16.  *    int wgetflag(id);
  17.  *    char *id;
  18.  *
  19.  *    char *wgetstr(id);
  20.  *    char *id;
  21.  *
  22.  */
  23. #include "obdefs.h"
  24. #include "gemdefs.h"
  25. #include "osbind.h"
  26. #include "defs.h"
  27. #include "hdx.h"
  28. #include "addr.h"
  29.  
  30.  
  31. #define    WBUFSIZE    1024
  32.  
  33. extern char toupper();
  34. extern char *getln();
  35. extern cgetln();
  36.  
  37.  
  38. char *wgetstr();
  39. char *wfindid();
  40.  
  41. static char wbuf[WBUFSIZE];
  42. static int wcapfd;
  43. static int wcapopen = 0;
  44.  
  45.  
  46. /*
  47.  * Get an entry into the buffer.
  48.  *
  49.  */
  50. wgetent(name, id)
  51. char *name;
  52. char *id;
  53. {
  54.     char *nm;
  55.  
  56.     if (wcapok() < 0) return ERROR;
  57.     wreset();
  58.  
  59.     if (id == NULL)
  60.     id = "mn";
  61.  
  62.     while (nextentry() == OK)
  63.     {
  64.     nm = wgetstr(id);
  65.     if (nm != NULL &&
  66.         !strcmp(nm, name)) return OK;
  67.     }
  68.     return ERROR;
  69. }
  70.  
  71.  
  72. /*
  73.  * Seek to beginning of wincap file
  74.  * and reset the line demon.
  75.  *
  76.  */
  77. wreset()
  78. {
  79.     Fseek(0L, wcapfd, 0);
  80.     getln(-1);
  81. }
  82.  
  83.  
  84. /*
  85.  * Get a list of all wcap entries;
  86.  * stuff into the buffer, seperated by nulls;
  87.  * final null marks last entry.
  88.  *
  89.  */
  90. wallents(buf, id)
  91. char *buf;
  92. char *id;
  93. {
  94.     char *nm;
  95.  
  96.     if (id == NULL)
  97.     id = "mn";
  98.  
  99.     if (wcapok() < 0) return ERROR;
  100.     wreset();
  101.  
  102.     while (nextentry() == OK)
  103.     {
  104.     if ((nm = wgetstr(id)) == NULL)
  105.         continue;
  106.     while (*nm)
  107.         *buf++ = *nm++;
  108.     *buf++ = '\0';
  109.     }
  110.  
  111.     *buf++ = '\0';
  112.     *buf = '\0';
  113.     return OK;
  114. }
  115.  
  116.  
  117. /*
  118.  * Get number from wcap entry.
  119.  *
  120.  */
  121. int wgetnum(id, anum)
  122. char *id;
  123. long *anum;
  124. {
  125.     char *ent;
  126.  
  127.     if ((ent = wfindid(id)) == NULL ||
  128.     *ent++ != '#')
  129.         return ERROR;
  130.     return getnum(ent, anum);
  131. }
  132.  
  133.  
  134. /*
  135.  * Get flag from wcap entry.
  136.  *
  137.  */
  138. wgetflag(id)
  139. char *id;
  140. {
  141.     char *ent;
  142.  
  143.     if ((ent = wfindid(id)) == NULL)
  144.         return ERROR;
  145.  
  146.     return OK;
  147. }
  148.  
  149.  
  150. /*
  151.  * Get string from wcap entry;
  152.  * return NULL if the id doesn't exist.
  153.  *
  154.  */
  155. char *wgetstr(id)
  156. char *id;
  157. {
  158.     char *ent;
  159.  
  160.     if ((ent = wfindid(id)) == NULL ||
  161.     *ent++ != '=')
  162.         return NULL;
  163.  
  164.     return ent;
  165. }
  166.  
  167.  
  168. /*
  169.  * Find an id;
  170.  * return pointer to rest of item (or NULL).
  171.  *
  172.  */
  173. char *wfindid(id)
  174. char *id;
  175. {
  176.     char *s;
  177.     char *skipstr();
  178.  
  179.     s = &wbuf[0];
  180.     s = skipstr(s);
  181.     while (*s)
  182.     {
  183.     if (s[0] == id[0] &&
  184.         s[1] != '\0' && id[1] != '\0' &&
  185.         s[1] == id[1])
  186.         return (s + 2);
  187.     s = skipstr(s);
  188.     }
  189.     return NULL;
  190. }
  191.  
  192.  
  193. /*
  194.  * Skip string;
  195.  * return ptr to thing past the string's `\0'.
  196.  *
  197.  */
  198. char *skipstr(s)
  199. char *s;
  200. {
  201.     while (*s) ++s;
  202.     return ++s;
  203. }
  204.  
  205.  
  206. /*
  207.  * Get next entry from wincap file;
  208.  * return ERROR if no more entries.
  209.  *
  210.  * Seperating `:'s are turned into \0,
  211.  * `\' can prevent that.
  212.  *
  213.  ***************** ***************** ***************** *****************
  214.  * Awww, shucks -- we need to do a "string delete" for that.  So '\'
  215.  * doesn't work yet.
  216.  ***************** ***************** ***************** *****************
  217.  *
  218.  */
  219. nextentry()
  220. {
  221.     char *s;
  222.  
  223.     while (cgetln(wcapfd, wbuf))
  224.     {
  225.     if (wbuf[0] == '#' ||        /* comment or empty line */
  226.         wbuf[0] == '\0')
  227.         continue;
  228.  
  229.     for (s = wbuf; *s; ++s)
  230.         switch (*s)
  231.         {
  232.         case '\\':    ++s;
  233.                 break;
  234.  
  235.         case ':':    *s = '\0';
  236.                 break;
  237.  
  238.         default:    continue;
  239.         }
  240.     *s++ = '\0';
  241.     return OK;
  242.     }
  243.  
  244.     return ERROR;
  245. }
  246.  
  247.  
  248. /*
  249.  * If the wcap file isn't open, then open it;
  250.  * return ERROR if the file won't open.
  251.  *
  252.  */
  253. wcapok()
  254. {
  255.     extern int running;
  256.  
  257.     if (!wcapopen &&
  258.     (wcapfd = (int)Fopen(WCAPFILE, 0)) < 0)
  259.     {
  260.     running = 0;
  261.     return err(nowincap);
  262.     }
  263.  
  264.     wcapopen = 1;
  265.     return OK;
  266. }
  267.  
  268.  
  269. /*
  270.  * Parse number;
  271.  *    octal numbers start with a leading `0';
  272.  *    decimal numbers start with digits;
  273.  *    hex numbers start with `0x' or `$';
  274.  *    numbers may be terminated with a `k' (1,000)
  275.  *    or `m' (1,000,000) multiplier.
  276.  *
  277.  */
  278. getnum(s, av)
  279. char *s;
  280. long *av;
  281. {
  282.     static char numtab[] = "0123456789abcdefABCDEF";
  283.     int base, i;
  284.     long v;
  285.  
  286.     base = 10;
  287.     v = 0L;
  288.     if(*s == '$')
  289.     {
  290.     ++s;
  291.     base = 16;
  292.     } else if (*s == '0' && toupper(s[1]) == 'X')
  293.     {
  294.     s += 2;
  295.     base = 16;
  296.     } else if (*s == '0')
  297.     base = 8;
  298.  
  299.     while(*s)
  300.     {
  301.     for(i = 0; numtab[i]; ++i)
  302.         if(*s == numtab[i]) break;
  303.     if(!numtab[i]) break;
  304.     if(base == 16 && i >= 16) i-=6;
  305.     if(i >= base) return ERROR;
  306.     v = (v * base) + i;
  307.     ++s;
  308.     }
  309.  
  310.  
  311.     /* handle multiplier */
  312.     switch (toupper(*s))
  313.     {
  314.     case 'K':
  315.         v *= 1024L;
  316.         break;
  317.  
  318.     case 'M':
  319.         v *= (1024L * 1024L);
  320.         break;
  321.  
  322.     case '\0':
  323.         break;
  324.  
  325.     default:
  326.         return ERROR;
  327.     }
  328.  
  329.     *av = v;
  330.     return OK;
  331. }
  332.